package com.yanqu.road.server.cmd.activity.rank;

import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.commonactivity.CommonActivityProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = CrossProtocol.C2_CROSS_GET_USER_ACTIVITY_RANK,desc = "跨服活动个人榜单")
public class GetCrossActivityUserRankListCmd extends CrossCommand {
    private int activityId;
    private int conditionType;
    private long userId;
    private int onlyTopNum;
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListReqMsg reqMsg = CommonActivityProto.CommonActivityRankListReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getRankType();
        userId = packet.getPlayerId();
        if (reqMsg.hasOnlyTopNum()) {
            onlyTopNum = reqMsg.getOnlyTopNum();
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CommonActivityProto.CommonActivityRankListRespMsg.Builder respMsg = CommonActivityProto.CommonActivityRankListRespMsg.newBuilder();
        respMsg.setMyRank(-1);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        if (rankListModel != null) {
            RankProto.RankListMsg.Builder rankListMsg = rankListModel.getRankListMsg();
            RankProto.RankListMsg.Builder tempMsg = RankProto.RankListMsg.newBuilder();
            int num = 0;
            for (RankProto.RankTempMsg msg : rankListMsg.getRankListList()) {
                if (conditionType == eGamePlayerEventType.CatActivityDamageRank.getValue() && msg.getValue().equals("0")) {
                    continue;
                }
                tempMsg.addRankList(msg);
                num++;
                if (num >= ActivityMgr.getRankListShowNum()) {
                    break;
                }
                if (onlyTopNum > 0 && num >= onlyTopNum) {
                    break;
                }
            }
            respMsg.setRankList(tempMsg);
            respMsg.setMyRank(rankListModel.getMyRank(userId, serverId));
        }else {
            respMsg.setRankList(RankProto.RankListMsg.newBuilder());
        }

        if (onlyTopNum > 0){ //只发前N名要用另外一个回包,不然客户端会有问题
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ACTIVITY_RANK_LIST_TOP_THREE, respMsg);
            MessageHelper.sendPacket(serverId, userId, message);
        }else{
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_ACTIVITY_RANK_LIST, respMsg);
            MessageHelper.sendPacket(serverId, userId, message);
        }
    }

    @Override
    public void sendData() {

    }
}
